package com.lq;

import com.lq.common.SqlConstant;
import com.lq.service.DataService;
import com.lq.service.InitService;
import com.lq.utils.CommonUtil;
import com.lq.utils.jdbc.Jdbc;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainApplication {

    public static Logger logUnique = Logger.getLogger("unique");
    public static Logger logHost = Logger.getLogger("host");
    public static Logger logDevice = Logger.getLogger("device");
    public static Logger logFloor = Logger.getLogger("floor");
    public static Logger logRecord = Logger.getLogger("record");
    public static Logger logGps = Logger.getLogger("gps");
    public static Logger logCXRecord = Logger.getLogger("cxrecord");
    public static Logger logTask = Logger.getLogger("task");
    public static Logger logRepalr = Logger.getLogger("repalr");


    public static void main(String[] args) {
        org.apache.log4j.PropertyConfigurator.configure(System.getProperty("user.dir") + "/config/log4j.properties");
        com.lq.utils.jdbc.PropertyConfigurator.configure("config/lqjdbc.properties");
        Map<String, Map> propsMap = Jdbc.getPropsDSMap("default");

        //获取服务地址接口IP
        String serverIp = propsMap.get("ServerIp") + "";
        //获取请求超时时间
        int reqTimeout = Integer.parseInt(propsMap.get("ReqTimeout") + "");
        //1:云平台同步数据 2:私有化同步数据
        String terraceType = propsMap.get("terraceType") + "";
        //true 正式  false 测试
        Boolean isOfficial = Boolean.getBoolean(propsMap.get("isOfficial") + "");
        //企业标识
        String companyKey = propsMap.get("companyKey") + "";
        //获取数据长度
        String dataLength = propsMap.get("dataLength") + "";
        //企业编号
        String companyCode = propsMap.get("companyCode") + "";
        //库区编号
        String areaCode = propsMap.get("areaCode") + "";
        //创建数据库表
        InitService.createTable();
        InitService.initTrackPingTai();

        DataService service = new DataService();
        /**
         * 设备实时数据（甘肃红十字血液中心专用）
         */
        /*new Thread(() -> {
            while (true) {
                try {
                    service.getRealTimeListData(serverIp, reqTimeout, isOfficial, companyCode, areaCode);
                } catch (Exception e) {
                    e.printStackTrace();
                    logUnique.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();*/


        /**
         * 设备信息
         */
        /*new Thread(() -> {
            while (true) {
                try {
                    service.getEquipmentListData(serverIp, reqTimeout, terraceType, companyKey, isOfficial);
                } catch (Exception e) {
                    e.printStackTrace();
                    logDevice.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();*/


        /**
         * 设备唯一标识，5分钟
         */
        new Thread(() -> {
            while (true) {
                try {
                    service.getEquipmentUniqueListData(serverIp, reqTimeout, isOfficial);
                } catch (Exception e) {
                    e.printStackTrace();
                    logUnique.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(300000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 设备主机，新结构
         */
        new Thread(() -> {
            while (true) {
                try {
                    service.getEquipmentHostListData(serverIp, reqTimeout, terraceType, companyKey, isOfficial);
                } catch (Exception e) {
                    e.printStackTrace();
                    logHost.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 设备信息，新结构
         */
        new Thread(() -> {
            while (true) {
                try {
                    service.getEquipmentInfoListData(serverIp, reqTimeout, terraceType, companyKey, isOfficial);
                } catch (Exception e) {
                    e.printStackTrace();
                    logDevice.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 设备信息，分层数据
         */
        new Thread(() -> {
            while (true) {
                try {
                    service.getEquipmentLayeredListData(serverIp, reqTimeout, terraceType, companyKey, isOfficial);
                } catch (Exception e) {
                    e.printStackTrace();
                    logFloor.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 历史数据
         */
        new Thread(() -> {
            Map<String, ListOrderedMap> mapCompany = new HashMap();
            while (true) {
                try {
                    List<ListOrderedMap> selectCompanyList = Jdbc.find(SqlConstant.SQL_SELECT_COMPANY, new Object[]{});
                    selectCompanyList.stream().forEach(l -> {
                        mapCompany.put(l.get("company_code").toString(), l);
                    });
                    //请求数据
                    service.getRecordListData(serverIp, reqTimeout, mapCompany, terraceType, companyKey, isOfficial, dataLength);
                } catch (Exception e) {
                    e.printStackTrace();
                    logRecord.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12004);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 超限数据
         */
        new Thread(() -> {
            Map<String, ListOrderedMap> mapCompany = new HashMap();
            while (true) {
                try {
                    List<ListOrderedMap> selectCompanyList = Jdbc.find(SqlConstant.SQL_SELECT_COMPANY, new Object[]{});
                    selectCompanyList.stream().forEach(l -> {
                        mapCompany.put(l.get("company_code").toString(), l);
                    });
                    service.getCXRecordListData(serverIp, reqTimeout, mapCompany, terraceType, companyKey, isOfficial, dataLength);
                } catch (Exception e) {
                    e.printStackTrace();
                    logCXRecord.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12006);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 定位数据
         */
        new Thread(() -> {
            Map<String, ListOrderedMap> mapCompany = new HashMap();
            while (true) {
                try {
                    List<ListOrderedMap> selectCompanyList = Jdbc.find(SqlConstant.SQL_SELECT_COMPANY, new Object[]{});
                    selectCompanyList.stream().forEach(l -> {
                        mapCompany.put(l.get("company_code").toString(), l);
                    });
                    service.getGpsListData(serverIp, reqTimeout, mapCompany, terraceType, companyKey, isOfficial, dataLength);
                } catch (Exception e) {
                    e.printStackTrace();
                    logGps.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12008);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 事件数据
         */
        new Thread(() -> {
            Map<String, ListOrderedMap> mapCompany = new HashMap();
            while (true) {
                try {
                    List<ListOrderedMap> selectCompanyList = Jdbc.find(SqlConstant.SQL_SELECT_COMPANY, new Object[]{});
                    selectCompanyList.stream().forEach(l -> {
                        mapCompany.put(l.get("company_code").toString(), l);
                    });
                    service.getEventsListData(serverIp, reqTimeout, mapCompany, terraceType, companyKey, isOfficial, dataLength);
                } catch (Exception e) {
                    e.printStackTrace();
                    logGps.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12010);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 设备任务数据
         */
        new Thread(() -> {
            Map<String, ListOrderedMap> mapCompany = new HashMap();
            while (true) {
                try {
                    List<ListOrderedMap> selectCompanyList = Jdbc.find(SqlConstant.SQL_SELECT_COMPANY, new Object[]{});
                    selectCompanyList.stream().forEach(l -> {
                        mapCompany.put(l.get("company_code").toString(), l);
                    });
                    service.getTaskLogListData(serverIp, reqTimeout, mapCompany, terraceType, companyKey, isOfficial, dataLength);
                } catch (Exception e) {
                    e.printStackTrace();
                    logTask.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12012);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 设备报修数据
         */
        new Thread(() -> {
            while (true) {
                try {
                    service.getRepalrListData(serverIp, reqTimeout, terraceType, companyKey, isOfficial);
                } catch (Exception e) {
                    e.printStackTrace();
                    logRepalr.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(12014);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        /**
         * 处理异常数据， 3分钟执行一次
         */
        /*new Thread(() -> {
            while (true) {
                try {
                    DataGpibService.handleRecord();
                } catch (Exception e) {
                    e.printStackTrace();
                    logRepalr.info(CommonUtil.e(e));
                }
                try {
                    Thread.sleep(300000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();*/

    }
}